<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use think\Controller;
use think\Db;
use think\Request;

class AdminController extends BaseController
{
    /**
     * 管理员列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $admins = Admin::paginate(5);
        $groups = Db::table('t_admin')->alias('a')
            ->field('a.*,c.title,c.status')
            ->join('t_auth_group_access b', 'b.uid=a.id')
            ->join('t_auth_group c', 'c.id=b.group_id')
            ->select();
        return view('index', compact('admins', 'groups'));
    }

    /**
     * 添加
     * @param Request $request
     * @return \think\response\View|void
     * @throws \think\exception\DbException
     */
    public function add(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $validate = $this->validate($data, [
                'nickname|昵称' => 'require|unique:admin',
                'password|密码' => 'require',
                'email|邮箱' => 'require|email',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            $admin = new Admin();
            $data['password'] = md5($data['password']);
            $data['create_at'] = time();
            $data['update_at'] = time();
            $res = $admin->insert($data);
            if ($res) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败', 'add');
            }
        }
        $groups = AuthGroup::all();
        return view('add', compact('groups'));
    }

    /**
     * 修改
     * @param Request $request
     * @param $id
     * @return \think\response\View|void
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit(Request $request, $id)
    {
        $admin = Admin::get($id);
        if ($request->isPost()) {
            $data = $request->post();
            //删除原角色
            Db::name('auth_group_access')->where('uid', $id)->delete();
            $validate = $this->validate($data, [
                'nickname|昵称' => 'require',
                'email|邮箱' => 'require|email',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            if ($data['password'] !== null) {
                $data['password'] = md5($data['password']);
            }
            $data['update_at'] = time();
            $res = $admin->allowField(true)->save($data);
            $ids = $admin->groupId;
            //halt($ids);
            foreach ($ids as $k => $v) {
                Db::name('auth_group_access')->insert(['uid' => $id, 'group_id' => $v]);
            }
            if ($res) {
                return $this->success('修改成功', 'index');
            } else {
                return $this->error('修改失败', 'edit');
            }
        }
        $groups = AuthGroup::all();
        $auth = implode(',', Db::name('auth_group_access')->where('uid', $id)->column('group_id'));
        return view('edit', compact('admin', 'groups', 'auth'));
    }

    /**
     * 删除
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function del($id)
    {
        $admin = Admin::get($id);
        $auth = Db::name('auth_group_access')->where('uid', $id)->delete();
        $res = $admin->delete();
        if ($res && $auth) {
            return $this->success('删除成功', 'index');
        } else {
            return $this->error('删除失败', 'index');
        }
    }

    /**
     * 登录
     * @param Request $request
     * @return \think\response\View|void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $validate = $this->validate($data, [
                'nickname|昵称' => 'require',
                'password|密码' => 'require',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            $admin = Admin::where('nickname', $data['nickname'])->find();
            if ($admin && md5($data['password']) == $admin['password']) {
                session('admin', $admin->toArray());
                return $this->success('登录成功', 'index');
            } else {
                return $this->error('用户名或密码错误', 'login');
            }
        }
        return view('login');
    }

    /**
     * 注销
     */
    public function logout()
    {
        session(null);
        session(null, 'think');
        return $this->success('注销成功', 'login');
    }
}
